package cn.iocoder.yudao.module.member.alliance.service;

import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.service.CrudService;
import cn.iocoder.yudao.framework.business.basic.service.impl.BaseService;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.alliance.dal.dataobject.CloudAllianceApplyDO;
import cn.iocoder.yudao.module.member.alliance.dal.dataobject.CloudAlliancePackageDO;
import cn.iocoder.yudao.module.member.alliance.dal.mysql.CloudAllianceApplyMapper;
import cn.iocoder.yudao.module.member.alliance.dal.mysql.CloudAlliancePackageMapper;
import cn.iocoder.yudao.module.member.alliance.vo.CloudAlliancePackageQueryDTO;
import cn.iocoder.yudao.module.member.alliance.vo.CloudAlliancePackageSaveDTO;
import cn.iocoder.yudao.module.member.alliance.vo.CloudAlliancePackageVO;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * 云盟商缴费套餐 Service 实现类
 *
 * @author 山野羡民
 */
@Service
@Validated
public class CloudAlliancePackageServiceImpl extends BaseService<CloudAlliancePackageDO, CloudAlliancePackageVO, CloudAlliancePackageQueryDTO> implements CloudAlliancePackageService, CrudService<CloudAlliancePackageDO, CloudAlliancePackageVO, CloudAlliancePackageSaveDTO, CloudAlliancePackageQueryDTO> {
    @Resource
    private CloudAlliancePackageMapper cloudAlliancePackageMapper;
    @Resource
    private CloudAllianceApplyMapper cloudAllianceApplyMapper;

    @Override
    public CloudAlliancePackageVO getOne(Long userId) {
        CoreQueryWrapper<CloudAlliancePackageDO> wrapper = buildQueryWrapper(userId);
        wrapper.limitN(1);
        CloudAlliancePackageDO aDO = getInjectedMapper().selectOne(wrapper);
        return BeanUtils.toBean(aDO, CloudAlliancePackageVO.class);
    }

    @Override
    public List<CloudAlliancePackageVO> getList(Long userId, @Nullable Integer limit) {
        CoreQueryWrapper<CloudAlliancePackageDO> wrapper = buildQueryWrapper(userId);
        if (limit != null && limit > 0) {
            wrapper.limitN(limit);
        }
        List<CloudAlliancePackageDO> list = getInjectedMapper().selectList(wrapper);
        return BeanUtils.toBean(list, CloudAlliancePackageVO.class);
    }

    @Nonnull
    private CoreQueryWrapper<CloudAlliancePackageDO> buildQueryWrapper(Long userId) {
        CoreQueryWrapper<CloudAlliancePackageDO> wrapper = new CoreQueryWrapper<>();
        if (userId != null && userId > 0) {
            CloudAllianceApplyDO applyDO = cloudAllianceApplyMapper.selectOne(CloudAllianceApplyDO::getUserId, userId);
            if (applyDO != null) {
                wrapper.eq("type", applyDO.getType());
            }
        }
        wrapper.orderByAsc("sort");
        wrapper.orderByAsc("price");
        wrapper.orderByDesc("id");
        return wrapper;
    }

    @Nonnull
    @Override
    protected CoreQueryWrapper<CloudAlliancePackageDO> buildQueryWrapper(CloudAlliancePackageQueryDTO param) {
        CoreQueryWrapper<CloudAlliancePackageDO> wrapper = super.buildQueryWrapper(param);
        wrapper.likeIfPresent("name", param.getName());
        wrapper.eqIfPresent("type", param.getType());
        return wrapper;
    }

    @Override
    public CloudAlliancePackageMapper getInjectedMapper() {
        return cloudAlliancePackageMapper;
    }

    @Override
    public CloudAlliancePackageDO convertToEntity(CloudAlliancePackageSaveDTO param) {
        return BeanUtils.toBean(param, CloudAlliancePackageDO.class);
    }

}